{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fde4b4a7-8872-4f15-9299-48f23f018955",
   "metadata": {},
   "source": [
    "### **PR代码练习: Numpy数据的分割代码练习：train_test_split**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f5d9652-af07-4a5a-9116-a14b46286f8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X:\n",
      " [[7 8]\n",
      " [5 0]\n",
      " [1 8]\n",
      " [7 3]\n",
      " [0 0]]\n",
      "y:\n",
      " [1 1 0 1 1]\n",
      "X_train:\n",
      " [[5 0]\n",
      " [7 3]\n",
      " [0 0]]\n",
      "X_test:\n",
      " [[1 8]\n",
      " [7 8]]\n",
      "y_train:\n",
      " [1 1 1]\n",
      "y_test:\n",
      " [0 1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X=np.random.randint(0,9,(5,2)) # 随机创建5行2列数，模拟5个点\n",
    "y=np.random.randint(0,2,5)     # 随机生成5个元素列表，模拟5个点的类别\n",
    "\n",
    "# 按照6:4 的比例分割数据，生成训练的X_train,y_train和测试的X_test和y_test\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)\n",
    "print(\"X:\\n\",X)\n",
    "print(\"y:\\n\",y)\n",
    "print(\"X_train:\\n\",X_train)\n",
    "print(\"X_test:\\n\",X_test)\n",
    "print(\"y_train:\\n\",y_train)\n",
    "print(\"y_test:\\n\",y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "db6b7fea-9b7d-4319-a598-9e92743174c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X:\n",
      " [[2 6]\n",
      " [7 8]\n",
      " [6 2]\n",
      " [2 1]\n",
      " [8 3]]\n",
      "y:\n",
      " [0 1 1 1 0]\n",
      "X_train:\n",
      " [[2 6]\n",
      " [2 1]\n",
      " [8 3]]\n",
      "X_test:\n",
      " [[7 8]\n",
      " [6 2]]\n",
      "y_train:\n",
      " [1 1 0]\n",
      "y_test:\n",
      " [0 1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#from sklearn.model_selection import train_test_split\n",
    "\n",
    "#定义函数\n",
    "def train_test_split(X, y, test_size, random_state): \n",
    "    size_X = X.shape[0]   #读取数据个数\n",
    "    size_y = y.shape[0]\n",
    "    \n",
    "    tmp1 = int(size_X * test_size)\n",
    "    tmp2 = int(size_y * test_size)\n",
    "    result1 = random.sample(range(size_X), tmp1)\n",
    "    result2 = random.sample(range(size_y), tmp2)\n",
    "    #X\n",
    "    X_test = X[result1]\n",
    "    X_train = np.delete(X, result1, axis=0)\n",
    "    #y\n",
    "    y_test = y[result2]\n",
    "    y_train = np.delete(y, result2, axis=0)\n",
    "    \n",
    "    return X_train,X_test,y_train,y_test #返回值\n",
    "\n",
    "X=np.random.randint(0,9,(5,2)) # 随机创建5行2列数，模拟5个点\n",
    "y=np.random.randint(0,2,5)     # 随机生成5个元素列表，模拟5个点的类别\n",
    "\n",
    "# 按照6:4 的比例分割数据，生成训练的X_train,y_train和测试的X_test和y_test\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)\n",
    "print(\"X:\\n\",X)\n",
    "print(\"y:\\n\",y)\n",
    "print(\"X_train:\\n\",X_train)\n",
    "print(\"X_test:\\n\",X_test)\n",
    "print(\"y_train:\\n\",y_train)\n",
    "print(\"y_test:\\n\",y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7200ddd-3be6-4c8d-948b-15a2d5ff9b4b",
   "metadata": {},
   "source": [
    "#### 参考答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "917afede-6350-4396-8682-a34a5f0dc5ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X:\n",
      " [[4 8]\n",
      " [3 4]\n",
      " [8 1]\n",
      " [5 1]\n",
      " [8 7]]\n",
      "y:\n",
      " [0 0 0 0 1]\n",
      "X_train:\n",
      " [[8 7]\n",
      " [4 8]\n",
      " [3 4]]\n",
      "X_test:\n",
      " [[8 1]\n",
      " [5 1]]\n",
      "y_train:\n",
      " [1 0 0]\n",
      "y_test:\n",
      " [0 0]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "\n",
    "X = np.random.randint(0, 9, (5, 2))  # 随机创建5行2列数，模拟5个点\n",
    "y = np.random.randint(0, 2, 5)     # 随机生成5个元素列表，模拟5个点的类别\n",
    "\n",
    "\n",
    "def train_test_split(X, y, test_size, random_state=None):\n",
    "# '''\n",
    "# X -> 数据集\n",
    "# y -> 标签集\n",
    "# test_size -> 测试数据占有的分割比例\n",
    "# random_state -> 控制生成可控的随机值\n",
    "# '''\n",
    "    rd = np.random.RandomState(seed=random_state) # 生成随机状态控制器，生成可控随机数\n",
    "\n",
    "    # 获得分割后的随机索引值列表\n",
    "    size = len(X) # 获得数据总体长度\n",
    "    train_size = int(size*(1-test_size)) # 获得训练数据总体长度\n",
    "    idx = range(0, size) # 生成完整的索引值列表\n",
    "    train_idx = rd.choice(idx, train_size, replace=False) # 随机选取训练数据集的部分索引值列表\n",
    "    test_idx = list(set(idx) - set(train_idx)) # 生成测试集的索引值列表\n",
    "    # np.setdiff1d\n",
    "\n",
    "    # 索引值列表得到训练和测试数据集\n",
    "    X_train = X[train_idx]\n",
    "    X_test = X[test_idx]\n",
    "    y_train = y[train_idx]\n",
    "    y_test = y[test_idx]\n",
    "    return X_train, X_test, y_train, y_test\n",
    "\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.4, random_state=5)\n",
    "print(\"X:\\n\", X)\n",
    "print(\"y:\\n\", y)\n",
    "print(\"X_train:\\n\", X_train)\n",
    "print(\"X_test:\\n\", X_test)\n",
    "print(\"y_train:\\n\", y_train)\n",
    "print(\"y_test:\\n\", y_test)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
